﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFong.Contract;

namespace EFong.View.Proxy
{
    public static class ServiceProxyExtensions
    {
        #region BaseServiceProxy

        public static List<Role> GetAllEffectivedRole(this BaseServiceProxy proxy)
        {
            ServiceResult<List<Role>> roles = proxy.GetAllRole();

            if (!roles.IsSuccess)
                return null;

            return (from r in roles.ReturnValue
                    where r.Effective
                    select r).ToList();
        }

        public static List<RoleAuthorizedItem> GetRoleAuthorizedItems(this BaseServiceProxy proxy)
        {            
            ServiceResult<List<AuthorizedItem>> authorizedItems = proxy.GetAllAuthorizedItem();

            if (!authorizedItems.IsSuccess)
                return null;

            List<RoleAuthorizedItem> result = new List<RoleAuthorizedItem>();

            foreach (var item in authorizedItems.ReturnValue)
            {
                result.Add(new RoleAuthorizedItem 
                {
                    AuthorizedItem = item
                });
            }

            return result;
        }

        #endregion

        #region MasterServiceProxy
        public static List<Contract.Project> GetAllEffectivedProject(this MasterServiceProxy proxy)
        {
            ServiceResult<List<Contract.Project>> projects = proxy.GetAllProject();

            if (!projects.IsSuccess)
                return null;

            return (from p in projects.ReturnValue
                    where p.Effective
                    select p).ToList();
        }

        public static List<Contract.BankAccount> GetAllEffectivedBankAccount(this MasterServiceProxy proxy)
        {
            ServiceResult<List<Contract.BankAccount>> bankAccounts = proxy.GetAllBankAccount();

            if (!bankAccounts.IsSuccess)
                return null;

            return (from b in bankAccounts.ReturnValue
                    where b.Effective
                    select b).ToList();
        }
        #endregion

        public static List<FlightWay> FilterIsDelFlightWays(this List<FlightWay> flightWays)
        {
            return (from f in flightWays where !f.IsDelete select f).ToList();
        }

        public static List<FlightOrderDatail> GetReportData(this List<FlightOrderDatail> details,FlightOrder order) 
        {
            List<FlightOrderDatail> result = new List<FlightOrderDatail>(details);
            int temp;
            Math.DivRem(details.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    FlightOrderDatail detail = new FlightOrderDatail();
                    detail.FlightOrder = order;
                    result.Add(detail);
                }
            }

            return result;
        }

        public static List<PFlightOrderDetail> GetReportData(this List<PFlightOrderDetail> details)
        {
            List<PFlightOrderDetail> result = new List<PFlightOrderDetail>(details);
            int temp;
            Math.DivRem(details.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    PFlightOrderDetail detail = new PFlightOrderDetail();
                    result.Add(detail);
                }
            }

            return result;
        }

        public static List<TrainTicketOrderDetail> GetReportData(this List<TrainTicketOrderDetail> details, TrainTicketOrder order)
        {
            List<TrainTicketOrderDetail> result = new List<TrainTicketOrderDetail>(details);
            int temp;
            Math.DivRem(details.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    TrainTicketOrderDetail detail = new TrainTicketOrderDetail();
                    detail.日期 = string.Empty;
                    detail.TrainTicketOrder = order;
                    detail.DeliveryCost = 0;
                    result.Add(detail);
                }
            }

            return result;
        }

        public static List<PTrainOrderDetail> GetReportData(this List<PTrainOrderDetail> details)
        {
            List<PTrainOrderDetail> result = new List<PTrainOrderDetail>(details);
            int temp;
            Math.DivRem(details.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    PTrainOrderDetail detail = new PTrainOrderDetail();
                    detail.日期 = string.Empty;
                    detail.DeliveryCost = 0;
                    result.Add(detail);
                }
            }

            return result;
        }

        public static List<Contract.HotelBookingDetail> GetReportData(this List<Contract.HotelBookingDetail> details, Contract.HotelBooking order)
        {
            List<HotelBookingDetail> result = new List<HotelBookingDetail>(details);
            int temp;
            Math.DivRem(details.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    HotelBookingDetail detail = new HotelBookingDetail();
                    detail.离店日期 = string.Empty;
                    detail.入住日期 = string.Empty;
                    detail.HotelBooking = order;
                    result.Add(detail);
                }
            }
            return result;
        }

        public static List<Contract.OtherBusiness> GetReportData(this Contract.OtherBusiness order)
        {
            List<Contract.OtherBusiness> result = new List<Contract.OtherBusiness>();
            result.Add(order);
            int temp;
            Math.DivRem(result.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    Contract.OtherBusiness o = new Contract.OtherBusiness();
                    o.日期 = string.Empty;
                    result.Add(o);
                }
            }
            return result;
        }

        public static List<Contract.TouringParty> GetReportData(this Contract.TouringParty order)
        {
            List<Contract.TouringParty> result = new List<Contract.TouringParty>();
            result.Add(order);
            int temp;
            Math.DivRem(result.Count, 4, out temp);
            if (temp != 0)
            {
                for (int i = 0; i < 4 - temp; i++)
                {
                    Contract.TouringParty o = new Contract.TouringParty();
                    result.Add(o);
                }
            }
            return result;
        }

        public static List<Contract.Passenger> FilterUnEffecivePassenger(this List<Contract.Passenger> passengers)
        {
            return (from f in passengers where f.Effective select f).ToList();
        }

        public static List<Contract.JourneyOrder> GetJourneyOrderByType(this MasterServiceProxy proxy,string type)
        {
            ServiceResult<List<Contract.JourneyOrder>> journeyOrders = proxy.GetAllAvailableJourneyOrder();

            if (!journeyOrders.IsSuccess)
                return null;

            return (from b in journeyOrders.ReturnValue
                    where b.Type == type
                    select b).ToList();
        }

        public static List<Contract.InsurancePolicy> GetInsurancePolicyByType(this MasterServiceProxy proxy, string type)
        {
            ServiceResult<List<Contract.InsurancePolicy>> insurancePolicy = proxy.GetAllAvailableInsurancePolicy();

            if (!insurancePolicy.IsSuccess)
                return null;

            return (from b in insurancePolicy.ReturnValue
                    where b.Type == type
                    select b).ToList();
        }
    }
}
